import json
import pandas as pd
import pymongo
import requests
from get_field_name.config import *
from get_field_name.save_to_db import *
# 连接数据库
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
table = db['third_name']

def get_third_field_name_list():
    data = pd.DataFrame(list(table.find()))
    data = data[["id", "n", "first_field_name", "second_field_name"]]
    third_field_name_list = []
    for i in data.index:
        third_field_name_list.append({"id": data.loc[i][0], "third_name": data.loc[i][1], "first_name": data.loc[i][2], "second_name": data.loc[i][3]})
    # for n in third_field_name_list:
    #     print(n)
    return third_field_name_list

def get_fourth_field_name(url):
    try:
        response = requests.get(url, verify=False)
        if response.content:  # 返回成功
            return response.content
    except requests.ConnectionError as e:
        print('url出错', e.args)



if __name__ == '__main__':
    third_field_name_list = get_third_field_name_list()
    # 10752 11228 13337 19856 22731 25600 29027 32246 39988 42811 43492 44131
    for i in range(44131, len(third_field_name_list)):
        url = "https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=" + str(third_field_name_list[i]['id'])
        print("index:", i)
        print(third_field_name_list[i]['third_name'])
        tansformData = json.loads(get_fourth_field_name(url))
        fourth_filed_name = tansformData['ct']
        parent_field_name = tansformData['bc']
        # for pp in parent_field_name:
        #     print(pp)
        for ss in fourth_filed_name:
            ss['first_field_name'] = third_field_name_list[i]['first_name']
            ss['second_field_name'] = third_field_name_list[i]['second_name']
            ss['third_field_name'] = parent_field_name[0]
        try:
            save_data("fourth_name", fourth_filed_name)
        except Exception:
            print("index:", i)